﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json;
using System.Diagnostics;



namespace COC_helper
{

    public partial class CardEdite : Form
    {
        public CardEdite()
        {
            InitializeComponent();
        }
       


        public MyCard mainJsonReader = new MyCard(); //存储卡牌信息



        /// <summary>
        /// 将数据压入文件
        /// </summary>
        public MyCard JsonPush()
        {
            string path = "asd.json";
            path = Global_Value.ZMB_PATH + path;
            try
            {
                using (StreamReader Reader = new StreamReader(path))
                {
                    string Context = Reader.ReadToEnd();
                    //Debug.Print(Context);
                    MyCard MainJsonReader = JsonConvert.DeserializeObject<MyCard>(Context);
                    return MainJsonReader;

                }
            }
            catch (Exception e)
            {
                Debug.Print("ERROR");
                Debug.WriteLine(e.Message);

                throw;
            }

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (MyCard card in Global_Value.zmb_Setting.MyCard)
                {
                    if (card.name == CardListBox.SelectedItem.ToString())
                    {
                        Global_Value.zmb_Setting.MyCard.Remove(card);
                        CardListBox.Items.Remove(CardListBox.SelectedItem);
                    }
                }
            }
            catch (Exception)
            {

                
            }
            


        }

        private void CardListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CardListBox.SelectedItem == null)
            {
                Debug.WriteLine("list指向异常！");
                return;
            }
            string curItem = CardListBox.SelectedItem.ToString();
            //MessageBox.Show(curItem);
            foreach(MyCard card in Global_Value.zmb_Setting.MyCard)
            {
                if (card.name.ToString() == curItem)
                { 
                    CardName.Text = card.name.ToString();
                    CardGorup.Text = card.group.ToString();
                    CardNeed.Text = card.need.ToString();
                    CardBuff.Text = card.buff.ToString();
                    CardInfo.Text = card.info.ToString();
                    textBox_PIC.Text = card.path.ToString();
                    try
                    {
                        pictureBox1.Image = Image.FromFile(Global_Value.ZMB_PATH + card.path);
                    }
                    catch (Exception err)
                    {
                        pictureBox1.Image = null;
                        Debug.WriteLine("图片加载失败了，因为" + err.ToString());
                        return;
                        //throw;
                    }
                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CardBuff.Text = "";
            CardName.Text = "";
            CardNeed.Text = "";
            CardInfo.Text = "";
            textBox_PIC.Text = "";
            button1.Visible = false;
            button3.Visible = true;
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (CardBuff.Text == "")
            { MessageBox.Show("没有规定卡片增益", "信息问题？"); return; }
            if (CardName.Text == "")
            { MessageBox.Show("没有规定卡片名字", "信息问题？"); return; }
            if (CardNeed.Text == "")
                CardNeed.Text = "NONE";
            if (CardInfo.Text == "")
            { MessageBox.Show("没有规定卡片信息", "信息问题？"); return; }
            if(textBox_PIC.Text == "")
            { MessageBox.Show("没有规定卡片封面", "信息问题？"); return; }

            button1.Visible = true;
            button3.Visible = false;
            MyCard temp = new MyCard();
            temp.name = CardName.Text;
            temp.need = CardNeed.Text;
            temp.info = CardInfo.Text;
            temp.group = CardGorup.Text;
            temp.path = textBox_PIC.Text;
            temp.buff = CardBuff.Text;


           CardListBox.Items.Add(temp.name);
            Global_Value.zmb_Setting.MyCard.Add(temp);  
            //string Json = JsonConvert.SerializeObject(mainJsonReader, Formatting.Indented);
            //Debug.WriteLine(Json);
            //try
            //{
            //    File.WriteAllText(TextBox_FilePath.Text + textBox_FlieName.Text, Json);
            //}
            //catch (Exception err)
            //{
            //    Debug.WriteLine("文件没有成功保存，因为：" +err.ToString());
            //    throw;
            //}
            

        }
  
        private void button_OpenJPG_Click(object sender, EventArgs e)
        {
          
            openFileDialog1.Title = "打开图片文件";
            openFileDialog1.Multiselect = false;
            openFileDialog1.InitialDirectory = TextBox_FilePath.Text;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_PIC.Text = openFileDialog1.SafeFileName;
                try
                {
                    pictureBox1.Image = Image.FromFile(Global_Value.ZMB_PATH + openFileDialog1.SafeFileName);

                }
                catch (Exception err)
                {
                    Debug.WriteLine("图片加载失败了，因为" + err.ToString());
                    throw;
                }
            }

        }

        private void CardEdite_Shown(object sender, EventArgs e)
        {
            this.BackColor = Color.FromArgb(255, 244, 192);
            Debug.Print("info:");

            //mainJsonReader = JsonPush(); //从文件载入，注意会丢失当前数据，你不应该在保存数据前调用
            //temp.CardList.ForEach(card => {
            //    Debug.Print(card.info);
            //    Debug.Print("NEXT");
            //});
            bool temp = true;

            foreach (MyCard card in Global_Value.zmb_Setting.MyCard)
            {
                temp = false;
                Debug.Print("next:");
                Debug.WriteLine(card.info);
                CardListBox.Items.Add(card.name);
                TextBox_FilePath.Text = Global_Value.ZMB_PATH;

            }
            if (temp)
                Debug.Print("读取了文件，但是没有读取到卡片，文件很可能有问题");
        }

        private void Button_Using_Click(object sender, EventArgs e)
        {
            if (CardListBox.SelectedItem == null)
                return;
            foreach (MyCard card in Global_Value.zmb_Setting.MyCard)
            {
                if (card.name == CardListBox.SelectedItem.ToString())
                {
                    Global_Value.zmb_Setting.fontCrad = card;
                    MessageBox.Show("已经成功将当前卡片设置为前台卡片");
                }
            }
        }
    }
}
